<template>
  <div class="app-container">
    <el-card shadow="never" class="mb20">
      <div class="filter-container">
        <div class="filter-item">
          <label class="radio-label">模型名称：</label>
          <el-input v-model="listQuery.modelName" clearable placeholder="模型名称" style="width: 200px;"  @keyup.enter.native="handleFilter" />
        </div>
        <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter"></el-button>
        <el-button class="filter-item" icon="el-icon-refresh-left" @click="handleReset"></el-button>
      </div>
    </el-card>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      row-key="id"
      border
      stripe
      fit
      highlight-current-row>
      <el-table-column prop="name" label="模型名称" min-width="200" align="center">
      </el-table-column>
      <el-table-column label="对应表单" min-width="200" align="center">
        <template slot-scope="scope">
          <ViewTableColumn :tableId="scope.row.tableId" :tableComment="scope.row.tableComment" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="部署时间" min-width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.deployTime | moment('YYYY-MM-DD HH:mm:ss') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180" align="center">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" circle icon="el-icon-caret-right" title="发起" @click="handleStart(scope.row)"></el-button>
          <el-button type="primary" size="mini" circle icon="el-icon-edit" title="修改" @click="handleEdit(scope.row)"></el-button>
          <el-button type="danger" size="mini" circle icon="el-icon-delete" title="删除" @click="handleRemove(scope.row)"></el-button>
          <!-- <el-button
            type="primary" 
            size="mini" 
            circle 
            icon="el-icon-copy-document" 
            title="复制页面链接" 
            v-clipboard:copy="`${copyUrl}?procdefId=${scope.row.procdefId}&deploymentId=${scope.row.id}`"
            v-clipboard:success="onCopy"
            v-clipboard:error="onCopyError">
          </el-button> -->
          <el-popover
            placement="left"
            title="流程参数">
              <div>
                <p>菜单名称: process</p>
                <p>菜单参数:
                  <br>
                  键: definitionKey
                  <br>
                  值: {{scope.row.key}}</p>
                <!-- <p>procdefId: {{scope.row.procdefId}}</p>
                <p>deploymentId: {{scope.row.id}}</p> -->
              </div>
            <el-button
              slot="reference"
              type="primary" 
              size="mini" 
              circle 
              icon="el-icon-info"
              title="点击查看参数"
              style="margin-left: 10px;">
          </el-button>
          </el-popover>
          
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNo" :limit.sync="listQuery.pageSize" @pagination="fetchData" />
  </div>
</template>

<script>
import { getPageList, remove } from '@/api/workflow/bpmn/deploy'
import Pagination from '@/views/workflow/components/Pagination'
import ViewTableColumn from '@/views/workflow/form/components/ViewTableColumn'

export default {
  components: { 
    Pagination,
    ViewTableColumn
  },
  data() {
    return {
      total: 0,
      listQuery: {
        modelName: undefined,
        pageNo: 1,
        pageSize: 10
      },
      list: [],
      listLoading: true,
      copyUrl: '/workflow/bpmn/process'
    }
  },
  filters: {},
  created() {
    this.fetchData()
  },
  methods: {
    handleFilter() {
      this.listQuery.pageNo = 1
      this.fetchData()
    },
    handleReset() {
      this.listQuery.modelName = ''
      this.handleFilter()
    },
    fetchData() {
      this.listLoading = true
      getPageList(this.listQuery).then(response => {
        this.total = response.data.total
        this.list = response.data.records
        this.listLoading = false
      })
    },
    handleStart(row) { // 发起流程
      const deploymentId = row.id
      this.$router.push({ path: '/workflow/bpmn/process', query: { procdefId: row.procdefId, deploymentId: deploymentId }})
    },
    handleEdit(row) {
      this.$router.push({ path: '/workflow/bpmn/create', query: { deploymentId: row.id }})
    },
    handleRemove(row) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: `您确定删除[${row.name}]这条记录吗？`,
        okText: '确定',
        okType: 'danger',
        cancelText: '取消',
        centered: true,
        onOk() {
          remove(row.id).then(res => {
            _this.$message.success('删除成功')
            _this.handleFilter()
          })
        }
      })
    },
    onCopy() {
      this.$message.success('复制成功')
    },
    onCopyError() {
      this.$message.console.error('复制失败')
    }
  }
}
</script>
